<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Cruises extends MY_Controller {

    function index() {
        //$this->output->enable_profiler(TRUE);
        $data['logged_in']  = $this->logged_in;
        $data['is_admin']   = $this->is_admin;
        $data['first_name'] = $this->first_name;
            
        $data['title'] = "Central Alabama VW Club -- Cruises";
        $data['heading'] = "Cruises";
		
        $this->load->library('table');

        $data['cruise_future_list'] = $this->_get_future_cruises();
        $data['cruise_past_list'] = $this->_get_past_cruises();

        $data['view'] = 'cruises_view';
        $this->load->vars($data);
        $this->load->view('master_view', $data);
    }

    function view_cruise_photos() {
        define("ID_SEGMENT", 3);

        $data['logged_in']  = $this->logged_in;
        $data['is_admin']   = $this->is_admin;
        $data['first_name'] = $this->first_name;

        $cruise_id = $this->uri->segment(ID_SEGMENT);
        if ($cruise_id == '') {
            echo 'No cruise selected.<br>';
            echo '<a href="'.base_url().'index.php?/cruises">Return</a>';
            return;
        }
        
        $data['additional_javascript_references'] = '<script type="text/javascript" src="'.base_url().'js/jquery.colorbox-min.js"></script>';
        $data['additional_javascript_doc_ready_functions'] = '$(".carGallery").colorbox({rel:\'carGallery\', width:"75%", height:"75%"});';
        $data['additional_css_references'] = link_tag('css/colorbox.css'.'?'.time());
        $data['cruise_gallery'] = $this->_get_cruise_gallery($cruise_id);
        
        $data['title'] = 'Cruise Gallery';
        $data['view'] = 'cruise_gallery_view';
        $this->load->vars($data);
        $this->load->view('master_view', $data);
        
        return;
    }
    function add_to_gallery(){
        
        if (!$this->is_admin) {
            echo 'Only site administrator allowed here<br>';
            echo '<a href="'.base_url().'">Go Back</a>';
            return;
        }
        
        $data['logged_in']  = $this->logged_in;
        $data['is_admin']   = $this->is_admin;
        $data['first_name'] = $this->first_name;
        
        $data['title'] = "Central Alabama VW Club -- Process Ftp'd images";
        $data['heading'] = "Cruise Photos Uploaded";
        
        $imageCount = 0;
        $dir = new DirectoryIterator($_SERVER['DOCUMENT_ROOT'].'/'.GALLERY_TEMP_UPLOAD_DIRECTORY);

        foreach($dir as $file ) {
            $filename = $file->getFilename();
            if ($filename !== '.' && $filename !== '..' && $filename !== '.svn')
                $imageCount ++; //Assuming everything is an image for now
        }
        $data['image_count'] = $imageCount;
        
        $this->load->model('Cruise_model');
        
        $cruise_selection_list = array();
        $cruise_selection_list[0] = '';
        foreach ($this->Cruise_model->Get_cruise_selection_list() as $row) {
            $cruise_selection_list[$row['cruise_id']] = $row['cruise'];
       }
        $data['cruise_selection_list'] = $cruise_selection_list; 
        
        $data['view'] = 'cruises_ftp';
        $this->load->vars($data);
        $this->load->view('master_view', $data);
        
    }
    
    function add_new_cruise(){
        
        
		$cruise_date = $this->input->post('cruise_date');
		if (strtotime($cruise_date) !== false)	
			$cruise_date = $this->make_us_date_mysql_friendly($cruise_date);
		else
			$cruise_date = null;

		$data = array(	
                        'name'          =>	$this->input->post('name'),
						'place'         =>	$this->input->post('place'),			
						'cruise_date'	=>	$cruise_date,
						'note'			=>	$this->input->post('note')
					);
        
        $has_images = $this->input->post('has_images');
        
        $this->load->model('Cruise_model');
        
        $cruise_id = $this->Cruise_model->Add_cruise($data);
        if (!$has_images)
            return;
        
        $cruise_name = $this->make_file_name_friendly($data['name']);
        $this->_process_uploaded_images($cruise_id, $cruise_name);
            
        return;
    }
    function store_images_in_cruise(){
        $cruise_id = $this->input->post('cruise_selection');
        if ($cruise_id == '0') {
            echo 'no cruise selected';
            return;
        }
        
        $this->load->model('Cruise_model');
        $cruise_info = $this->Cruise_model->Load_cruise_info($cruise_id);
        $cruise_name = $cruise_info[0]['name'];
        $cruise_name = $this->make_file_name_friendly($cruise_name);
        $this->_process_uploaded_images($cruise_id, $cruise_name);
        
        echo '<br>you made it<br>'.$cruise_id;
    }
    
    function _get_cruise_gallery($cruise_id) {
        
        $this->load->model('Cruise_model');
	$cruise_list_array = $this->Cruise_model->Get_cruise_gallery($cruise_id);
       
        
        $gallery_html   =   '';
        $gallery_html   .=  '<table class="member-gallery-grid">';
		       
	//loop through the results and checkbox the id
        $row_pic_count  =   0;
        $image_row      =   '';
        $start_count    =   false;
	foreach($cruise_list_array as $row) {
           if ($start_count)
            $row_pic_count++;
           else
            $start_count =   true;
           
           if ($row_pic_count == MAX_ROW_PICS) {
               $gallery_html .= '<tr>'.$image_row.'</tr>';
               $image_row = '';
               $row_pic_count = 0;
           }
               
           $thumb_link  = '<img src="./cruise_gallery/thumbnail/'.str_replace('.jpg','_thumb.jpg',$row['pic_name']).'"/>';
           $source_link = base_url().'/cruise_gallery/'.$row['pic_name'].'"';
           $image_anchor = anchor($source_link,$thumb_link,'class = carGallery');
           $image_row   .=   '<td class="image-cell">'.$image_anchor.'</td>';
	}
        
	$gallery_html .= '<tr>'.$image_row.'</tr>';
        $gallery_html .= '</table>';
		
		
	return $gallery_html;

    }
    function _process_uploaded_images($cruise_id, $name) {
        $dir = new DirectoryIterator($_SERVER['DOCUMENT_ROOT'].'/'.GALLERY_TEMP_UPLOAD_DIRECTORY);
        foreach($dir as $file ) {
            $pathname = $file->getPathname();
            $file_name = $file->getFilename();
            if ($file_name !== '.' && $file_name !== '..' && $file_name !== '.svn') {
                echo 'processing'.$pathname.'<br>';
                $new_image_name = $this->process_image($pathname, 'cruise_gallery', 600, 700, 150, true, $cruise_id.'_'.$name);
                echo 'new image name'.$new_image_name.'<br>';
                $pic_data = array(	
                        'cruise_id'          =>	$cruise_id,
						'pic_name'      => $new_image_name
					);
                $cruise_gallery_id = $this->Cruise_model->Add_cruise_gallery_image($pic_data);
            }
        }
        
        
    }

	function _get_future_cruises(){
        
		$this->table->set_heading('Cruise Date','Days til Cruise','Name','Place','Note');
		
		$this->load->model('Cruise_model');
		$cruise_array = $this->Cruise_model->Load_cruise_info();
		
		$local_table_template = set_table_template();
		$this->table->set_template($local_table_template); 
		
		return $this->table->generate($cruise_array);
		
	}
        
	function _get_past_cruises(){
        
		$this->table->set_heading('Gallery','Cruise Date','Name','Place','Images');
		
		$this->load->model('Cruise_model');
		$cruise_array = $this->Cruise_model->Load_past_cruise_info();
                
                foreach($cruise_array as $row) {
                    if($row['image_count'] > 0)
                        $row['cruise_id'] = anchor(base_url().'index.php?/cruises/view_cruise_photos/'.$row['cruise_id'], 'See Photos');
                    else
                        $row['cruise_id'] = 'No photos yet';
                    
                    $this->table->add_row($row);
		}
		
		$local_table_template = set_table_template();
		$this->table->set_template($local_table_template); 
		
		return $this->table->generate();
	}
	

}

/* End of file cruises.php */
/* Location: ./application/controllers/cruises.php */